package com.mikepenz.iconics.sample.slice;

import ohos.aafwk.ability.AbilitySlice;
import ohos.aafwk.content.Intent;
import ohos.agp.components.AbsButton;
import ohos.agp.components.AttrHelper;
import ohos.agp.components.Component;
import ohos.agp.components.ComponentState;
import ohos.agp.components.Image;
import ohos.agp.components.RadioButton;
import ohos.agp.utils.Color;
import ohos.media.image.common.Size;

import com.mikepenz.iconics.IconicsDrawable;
import com.mikepenz.iconics.IconicsDrawableConverter;
import com.mikepenz.iconics.sample.ResourceTable;
import com.mikepenz.iconics.view.IconicsImageView;

import java.util.HashMap;

/**
 * java代码和xml配置，类库相关使用方法
 *
 * @since 2021-06-16
 */
public class PlaygroundAbilitySlice extends AbilitySlice {
    private static final int COLOR1 = 0xff2e7d32;
    private static final int COLOR2 = 0xffb388ff;
    private static final int COLOR3 = 0xffa5d6a7;
    private static final int COLOR4 = 0xff37474f;
    private static final int COLOR5 = 0xffF44336;
    private static final int COLOR6 = 0xaa00FF00;
    private static final int COLOR7 = 0xffffffff;
    private static final int COLOR8 = 0xff9e9e9e;
    private static final int COLOR9 = 0xff80deea;
    private static final int COLOR10 = 0xffffb300;
    private Image textBack;
    private RadioButton rb1;
    private RadioButton rb2;
    private RadioButton rb3;

    @Override
    public void onStart(Intent intent) {
        super.onStart(intent);
        super.setUIContent(ResourceTable.Layout_ability_playground);
        getWindow().setStatusBarColor(Color.getIntColor("#D32F2F"));
        initTitle();
        initButtonShowcase();
        rb1 = (RadioButton) findComponentById(ResourceTable.Id_md_nv1);
        rb2 = (RadioButton) findComponentById(ResourceTable.Id_md_nv2);
        rb3 = (RadioButton) findComponentById(ResourceTable.Id_md_nv3);
        initMdNav1();
        initMdNav2();
    }

    private void initMdNav1() {
        final int vpSize = 26;
        final Size size = new Size(AttrHelper.vp2px(vpSize, this), AttrHelper.vp2px(vpSize, this));
        IconicsDrawable iconicsDrawable1 = new IconicsDrawable(null, this);
        iconicsDrawable1.setIcon("cmd_history");
        iconicsDrawable1.setColor(new Color(COLOR1));
        rb1.setAroundElements(null, IconicsDrawableConverter.converter(iconicsDrawable1, size), null, null);
        rb1.setCheckedStateChangedListener(new AbsButton.CheckedStateChangedListener() {
            @Override
            public void onCheckedChanged(AbsButton absButton, boolean checked) {
                if (checked) {
                    iconicsDrawable1.setColor(new Color(COLOR2));
                } else {
                    iconicsDrawable1.setColor(new Color(COLOR1));
                }
                rb1.setAroundElements(null, IconicsDrawableConverter.converter(iconicsDrawable1, size), null, null);
            }
        });
        IconicsDrawable iconicsDrawable2 = new IconicsDrawable(null, this);
        iconicsDrawable2.setIcon("cmd_home");
        iconicsDrawable2.setColor(new Color(COLOR1));
        rb2.setAroundElements(null, IconicsDrawableConverter.converter(iconicsDrawable2, size), null, null);
        rb2.setCheckedStateChangedListener(new AbsButton.CheckedStateChangedListener() {
            @Override
            public void onCheckedChanged(AbsButton absButton, boolean checked) {
                if (checked) {
                    iconicsDrawable2.setColor(new Color(COLOR2));
                } else {
                    iconicsDrawable2.setColor(new Color(COLOR1));
                }
                rb2.setAroundElements(null, IconicsDrawableConverter.converter(iconicsDrawable2, size), null, null);
            }
        });
        IconicsDrawable iconicsDrawable3 = new IconicsDrawable(null, this);
        iconicsDrawable3.setIcon("cmd_calendar");
        iconicsDrawable3.setColor(new Color(COLOR1));
        rb3.setAroundElements(null, IconicsDrawableConverter.converter(iconicsDrawable3, size), null, null);
        rb3.setCheckedStateChangedListener(new AbsButton.CheckedStateChangedListener() {
            @Override
            public void onCheckedChanged(AbsButton absButton, boolean checked) {
                if (checked) {
                    iconicsDrawable3.setColor(new Color(COLOR2));
                } else {
                    iconicsDrawable3.setColor(new Color(COLOR1));
                }
                rb3.setAroundElements(null, IconicsDrawableConverter.converter(iconicsDrawable3, size), null, null);
            }
        });
        rb1.setChecked(true);
    }

    private void initMdNav2() {
        final int vpSize = 26;
        final Size size = new Size(AttrHelper.vp2px(vpSize, this), AttrHelper.vp2px(vpSize, this));
        IconicsDrawable iconicsDrawable4 = new IconicsDrawable(null, this);
        iconicsDrawable4.setIcon("gmd_favorite");
        iconicsDrawable4.setColor(new Color(COLOR3));
        RadioButton rb4 = (RadioButton) findComponentById(ResourceTable.Id_md_nv4);
        rb4.setAroundElements(null, IconicsDrawableConverter.converter(iconicsDrawable4, size), null, null);
        IconicsDrawable iconicsDrawable5 = new IconicsDrawable(null, this);
        iconicsDrawable5.setIcon("gmd_star");
        iconicsDrawable5.setColor(new Color(COLOR4));
        RadioButton rb5 = (RadioButton) findComponentById(ResourceTable.Id_md_nv5);
        rb5.setAroundElements(null, IconicsDrawableConverter.converter(iconicsDrawable5, size), null, null);
        rb5.setCheckedStateChangedListener(new AbsButton.CheckedStateChangedListener() {
            @Override
            public void onCheckedChanged(AbsButton absButton, boolean checked) {
                if (checked) {
                    iconicsDrawable5.setColor(new Color(COLOR5));
                } else {
                    iconicsDrawable5.setColor(new Color(COLOR4));
                }
                rb5.setAroundElements(null, IconicsDrawableConverter.converter(iconicsDrawable5,
                    size), null, null);
            }
        });
        rb4.setChecked(true);
    }

    private void initButtonShowcase() {
        IconicsImageView test6 = (IconicsImageView) findComponentById(ResourceTable.Id_test6);
        final IconicsDrawable icon6 = test6.getIcon();
        HashMap<Integer, Color> colorList6 = new HashMap<>();
        colorList6.put(ComponentState.COMPONENT_STATE_PRESSED, new Color(COLOR5));
        colorList6.put(ComponentState.COMPONENT_STATE_EMPTY, new Color(COLOR6));
        icon6.setColorList(colorList6);
        IconicsImageView test7 = (IconicsImageView) findComponentById(ResourceTable.Id_testn);
        final IconicsDrawable icon7 = test7.getIcon();
        HashMap<Integer, Color> colorList7 = new HashMap<>();
        colorList7.put(ComponentState.COMPONENT_STATE_PRESSED, new Color(COLOR7));
        colorList7.put(ComponentState.COMPONENT_STATE_EMPTY, new Color(COLOR8));
        icon7.setColorList(colorList7);
        HashMap<Integer, Color> bgContour7 = new HashMap<>();
        bgContour7.put(ComponentState.COMPONENT_STATE_PRESSED, new Color(COLOR5));
        bgContour7.put(ComponentState.COMPONENT_STATE_EMPTY, new Color(COLOR4));
        icon7.setBackgroundContourColorList(bgContour7);
        HashMap<Integer, Color> bgColorList7 = new HashMap<>();
        bgColorList7.put(ComponentState.COMPONENT_STATE_PRESSED, new Color(COLOR9));
        bgColorList7.put(ComponentState.COMPONENT_STATE_EMPTY, new Color(COLOR10));
        icon7.setBackgroundColorList(bgColorList7);
    }

    private void initTitle() {
        textBack = (Image) findComponentById(ResourceTable.Id_text_back);
        textBack.setClickedListener(new Component.ClickedListener() {
            @Override
            public void onClick(Component component) {
                terminateAbility();
            }
        });
    }
}
